import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

public class ButtonStrip extends JScrollPane {

	
	JPanel strip;
	/**
	 * 
	 */
	private static final long serialVersionUID = -1880102663316794293L;
	
	public String selObj = null;
	
	public HashMap<String, Class<?>> objs = new HashMap<String, Class<?>>();
	
	//private JScrollPane strip;
	
	//private ArrayList<Object> objects;

	public ButtonStrip() {
		super();
		setSize(new Dimension(100, 600));
		
		
		this.strip = getStrip();
		
        JViewport viewport = new JViewport();
        viewport.setBackground(Color.white);
        
        viewport.setView(this.strip);
		viewport.setSize(new Dimension(80, 600));
        setViewportView(viewport);
		
		setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
		setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
		
		setVisible(true);
		
		System.out.println("Creating button strip");
		
		//objects = new ArrayList<Object>();
		
		
		init();
	}
	
	
	
	private void init() {
		
		String currentDir = System.getProperty("user.dir");
		currentDir+="\\plugins\\Objects";
		
		File file = new File(currentDir);
		
		System.out.println("Classes in objects folder:");
		
		for (File fl : file.listFiles()) {
			System.out.println("file: "+fl.getName());
		}
		
		
		URL url = null;
		
		try {
			url = file.toURI().toURL();
			URL[] urls = new URL[]{url}; 
			ClassLoader cl = new URLClassLoader(urls); 
			
			System.out.println("Files in objects folder:");
			
			

			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			int i = 0;
			c.fill = GridBagConstraints.HORIZONTAL;
			
			for (File fl : file.listFiles()) {
				//System.out.println("file: "+fl.getName());
				c.gridy = i++;
				String name = fl.getName().substring(0, fl.getName().indexOf('.'));
				Class<?> tmpObj = cl.loadClass(name);
				
				//this.objects.add(tmpObj);
				this.objs.put(name, tmpObj);
				
				JButton tmp = new JButton(name);
				
				tmp.setIcon(new javax.swing.ImageIcon(currentDir+"\\"+name+".png"));
				tmp.addActionListener(new SelectClassCommand(this, name));
				
				this.strip.add(tmp, c);
				
				//++i++;
			}
			
			
			
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		/*for (Object ob : this.objects) {
			
		}*/
		
	
	}
	
	
	
	
	
	private JPanel getStrip() {
		if(strip == null) {
			
			GridBagLayout griBagLayout = new GridBagLayout();
			
			
			strip = new JPanel();
			strip.setLayout(griBagLayout);
			strip.setSize(new Dimension(80, 1000));
			

			
//			JButton tmp = new JButton("Meh");
//			tmp.addActionListener(new ActionListener() {
//				
//				@Override
//				public void actionPerformed(ActionEvent e) {
//					// TODO Auto-generated method stub
//					System.out.println("Meh");
//				}
//			});
//			tmp.setSize(new Dimension(64, 64));
//			strip.add(tmp);
//			
//			
//			tmp = new JButton("Quack");
//			tmp.setSize(new Dimension(64, 64));
//			strip.add(tmp);
//			
//			
//			tmp = new JButton("Moo");
//			tmp.setSize(new Dimension(64, 64));
//			strip.add(tmp);
			//strip.setPreferredSize(new Dimension(5, 5));
			
			
			
		}
		return strip;
	}
	
	
	
}
